package com.beiben.workflow.controller;

import com.beiben.workflow.domain.WfApply;
import com.beiben.workflow.domain.WfApplyVo;
import com.beiben.workflow.service.IWfApplyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作流申请Controller
 *
 * @author 熊国强
 * @date 2020-12-17
 */
@RestController
@RequestMapping("/workflow/apply")
public class WfApplyController extends BaseController {
    @Autowired
    private IWfApplyService wfApplyService;

    /**
     * 查询工作流申请列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(WfApply wfApply) {
        startPage();
        List<WfApply> list = wfApplyService.selectWfApplyList(wfApply);
        return getDataTable(list);
    }

    /**
     * 导出工作流申请列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:export')")
    @Log(title = "工作流申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WfApply wfApply) {
        List<WfApply> list = wfApplyService.selectWfApplyList(wfApply);
        ExcelUtil<WfApply> util = new ExcelUtil<WfApply>(WfApply.class);
        return util.exportExcel(list, "apply");
    }

    /**
     * 获取工作流申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId) {
        return AjaxResult.success(wfApplyService.selectWfApplyById(applyId));
    }

    /**
     * 新增工作流申请
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:add')")
    @Log(title = "工作流申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WfApply wfApply) {
        return toAjax(wfApplyService.insertWfApply(wfApply));
    }

    /**
     * 修改工作流申请
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:edit')")
    @Log(title = "工作流申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WfApply wfApply) {
        return toAjax(wfApplyService.updateWfApply(wfApply));
    }

    /**
     * 删除工作流申请
     */
    @PreAuthorize("@ss.hasPermi('workflow:apply:remove')")
    @Log(title = "工作流申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable Long[] applyIds) {
        return toAjax(wfApplyService.deleteWfApplyByIds(applyIds));
    }

    /**
     * 提交申请 并上传工作流
     * 前端 暂时未用 使用时 请更改此处注释
     */
    @Log(title = "工作流申请", businessType = BusinessType.INSERT)
    @PostMapping("/submitApplyCheck")
    public AjaxResult submitApplyCheck(@RequestBody WfApplyVo wfApply) {
        return AjaxResult.success(wfApplyService.submitApplyCheck(wfApply));
    }

    /**
     * 我的申请列表
     *
     * @return
     */
    @Log(title = "工作流申请")
    @GetMapping("/submitApplyList")
    public TableDataInfo submitApplyList(WfApplyVo wfApply) {
        startPage();
        List<WfApplyVo> list = wfApplyService.applyList(wfApply);
        return getDataTable(list);
    }

    /**
     * 提交申请 重新提交
     *
     * @return
     */
    @Log(title = "工作流申请")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody WfApply wfApply) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return toAjax(wfApplyService.submit(loginUser.getUser().getUserId(), wfApply));
    }

}